import { Tab, TabTitleText, Tabs } from '@patternfly/react-core';
import type { TabsProps } from '@patternfly/react-core';

import { isVulnerabilityState, vulnerabilityStates } from 'types/cve.proto';
import type { VulnerabilityState } from 'types/cve.proto';
import useURLStringUnion from 'hooks/useURLStringUnion';

export const vulnStateTabContentId = 'vulnerability-state-tab-content';

export type VulnerabilityStateTabsProps = {
    isBox?: TabsProps['isBox'];
    titleOverrides?: {
        observed?: string;
        deferred?: string;
        falsePositive?: string;
    };
    onChange?: (key: VulnerabilityState) => void;
};

function VulnerabilityStateTabs({
    titleOverrides,
    isBox = false,
    onChange,
}: VulnerabilityStateTabsProps) {
    const [vulnerabilityStateKey, setVulnerabilityStateKey] = useURLStringUnion(
        'vulnerabilityState',
        vulnerabilityStates
    );

    return (
        <Tabs
            activeKey={vulnerabilityStateKey}
            onSelect={(_e, tab) => {
                setVulnerabilityStateKey(tab);
                if (onChange && isVulnerabilityState(tab) && tab !== vulnerabilityStateKey) {
                    onChange(tab);
                }
            }}
            isBox={isBox}
        >
            <Tab
                eventKey="OBSERVED"
                title={<TabTitleText>{titleOverrides?.observed ?? 'Observed'}</TabTitleText>}
                tabContentId={vulnStateTabContentId}
            />
            <Tab
                eventKey="DEFERRED"
                title={<TabTitleText>{titleOverrides?.deferred ?? 'Deferred'}</TabTitleText>}
                tabContentId={vulnStateTabContentId}
            />
            <Tab
                eventKey="FALSE_POSITIVE"
                title={
                    <TabTitleText>
                        {titleOverrides?.falsePositive ?? 'False positives'}
                    </TabTitleText>
                }
                tabContentId={vulnStateTabContentId}
            />
        </Tabs>
    );
}

export default VulnerabilityStateTabs;
